package com.cskaoyan.service.promotionService;

import com.cskaoyan.bean.Topic;
import com.cskaoyan.bean.TopicExample;
import com.cskaoyan.bean.promotionVO.topicVO.TopicListVO;
import com.cskaoyan.mapper.TopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.sf.jsqlparser.statement.select.Top;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    TopicMapper topicMapper;


    @Override
    public TopicListVO topicList(Integer page, Integer limit, String sort, String order, String title, String subtitle) {
        PageHelper.startPage(page, limit);

        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andIdIsNotNull().andDeletedEqualTo(false);

        if (title != null & !"".equals(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (subtitle != null & !"".equals(subtitle)) {
            criteria.andSubtitleLike("%" + subtitle + "%");
        }

        List<Topic> topics = topicMapper.selectByExampleWithBLOBs(topicExample);

        PageInfo<Topic> topicPageInfo = new PageInfo<>(topics);
        long total = topicPageInfo.getTotal();

        TopicListVO topicListVO = new TopicListVO();
        topicListVO.setItems(topics);
        topicListVO.setTotal(total);
        return topicListVO;
    }

    @Override
    public Topic topicAdd(Topic topic) {
        topic.setAddTime(new Timestamp(System.currentTimeMillis()));
        topic.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        topicMapper.insertSelective(topic);
        Topic topicVO = topicMapper.selectByPrimaryKey(topic.getId());
        return topicVO;


    }

    @Override
    public Topic topicUpdate(Topic topic) {
        topic.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        topicMapper.updateByPrimaryKeyWithBLOBs(topic);
        Topic topicVO = topicMapper.selectByPrimaryKey(topic.getId());
        return topicVO;
    }

    @Override
    public int topicDel(Topic topic) {

        int code = topicMapper.updateByPrimaryKey(topic);
        return code;
    }
}
